home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17685 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  5.6 KB

  1. Path: newsfeed.concentric.net!news
  2. From: "Alan L. Lovejoy" <alovejoy@concentric.net>
  3. Newsgroups: comp.lang.java,comp.lang.c++,comp.lang.smalltalk
  4. Subject: Re: Will Java kill C++?
  5. Date: Wed, 17 Apr 1996 00:03:51 -0700
  6. Organization: Modulation
  7. Message-ID: <317497D7.884@concentric.net>
  8. References: <3134D499.653E@ix.netcom.com> <4kbfn8$1bu@news1.is.net> <4kqjf6$kh0@kaiwan009.kaiwan.com> <317173F1.5790@concentric.net> <Dpz6It.2An@undergrad.math.uwaterloo.ca>
  9. NNTP-Posting-Host: cnc009035.concentric.net
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.01Gold (Win95; U)
  14.  
  15. Carl Laurence Gonsalves wrote:
  16. > In article <317173F1.5790@concentric.net>,
  17. > Alan L. Lovejoy <alovejoy@concentric.net> wrote:
  18. > >Mike Zorn wrote:
  19. > >>
  20. > >> In <4kbfn8$1bu@news1.is.net> mvantassel@teambca.com (Mark VanTassel) writes:
  21. > >>
  22. > >> >"Alan L. Lovejoy" <alovejoy@concentric.net> wrote:
  23. > >> >>Bzzzt!  Not according to the benchmarks I've done.  Go benchmark the factorial or fibonacci
  24. > >> >>functions (implemented recursively) in both C and a good Smalltalk.  You are in for a big
  25. > >> >>surprise.
  26. > >
  27. > >> >I don't think this is a valid benchmark... (and I too fail to see how
  28. > >> >Smalltalk can be faster than C++ except perhaps in bizarre special
  29. > >> >cases)
  30. > >>    True, the Fibonacci and factorial are not good candidates for a
  31. > >> recursive algorithm.  (They're just taught that way because it's one
  32. > >> of the few recursive problems that we can understand in first-year
  33. > >> college courses.)
  34. > >>    On the other hand, almost any interesting program, compiled & run
  35. > >> in different languages, should be able to give a good idea of how the
  36. > >> languages compare.  For my part, I'd also like to look at the source
  37. > >> code, and know how long it took to write (and debug) the two programs.
  38. > >>    So, Alan, I guess you'll have to come up with a program for
  39. > >> Ackermann's Function in C++ and Smalltalk to satisfy the rest of us.
  40. > >>
  41. > >> Mike Zorn      ozma@kaiwan.com   |  Thought for the day:
  42. > >>   http://www.kaiwan.com/~ozma/   |    Java is C--
  43. > >
  44. > >I did those benchmars to prove that Smalltalk message sends are faster
  45. > >than c function calls.  To do that, I needed a benchmark that consisted
  46. > >mostly of function calls/message sends--and involved as little other
  47. > >code as possible.  The recursive factorial and fibonacci functions
  48. > >meet those requirements better than anything else I could think of.
  49. > Both factorial and fibonacci are tail-recursive, which means that your
  50. > translator may be doing tail recursion elimination, folding your n message
  51. > sends (or function calls) down to one (plus a loop).
  52. > I don't know of any C compilers that do tail recursion elimination. I
  53. > don't know enough about the workings of Smalltalk to say for sure, but it
  54. > may be possible that your translator is doing tail recursion elimination,
  55. > thereby making message sends seem to be a heck of a lot more efficient
  56. > than they really are.
  57. > Try doing a similar test with functions that aren't tail-recursive. Or at
  58. > least find out if your Smalltalk translator may be doing tail-recursion
  59. > elimination.
  60. > --
  61. >         Carl Laurence Gonsalves - clgonsal@undergrad.math.uwaterloo.ca
  62. >                    Computer Science, University of Waterloo
  63. >                http://www.undergrad.math.uwaterloo.ca/~clgonsal/
  64. >                    http://www.csclub.uwaterloo.ca/~clgonsal/
  65.  
  66. Here's the Smalltalk method source code:
  67.  
  68. factorial
  69.     "Answer the factorial of the receiver. Fail if the 
  70.     receiver is less than 0."
  71.  
  72.     self < 2
  73.         ifTrue: 
  74.             [self < 0 ifTrue: [self error: 'Must not be negative'].
  75.             ^1].
  76.     ^(self - 1) factorial * self
  77.  
  78. Here's the bytecodes for the same method:
  79.  
  80. 1 <44> push self
  81. 2 <4B> push 2
  82. 3 <A2> send <
  83. 4 <E8 0B> jump false 17
  84. 6 <44> push self
  85. 7 <49> push 0
  86. 8 <A2> send <
  87. 9 <C4> jump false 15
  88. 10 <44> push self
  89. 11 <1C> push 'Must not be negative'
  90. 12 <F0 20> send error:
  91. 14 <66> pop
  92. 15 <4A> push 1
  93. 16 <65> return
  94. 17 <44> push self
  95. 18 <4A> push 1
  96. 19 <A1> send -
  97. 20 <71> send factorial
  98. 21 <44> push self
  99. 22 <A8> send *
  100. 23 <65> return
  101.  
  102. I see no tail recursion elimination.  It's harder to do in Smalltalk, because 
  103. any message send could theoretically add, recompile, or remove any method to, 
  104. in or from any class. Message semantics (with some very specific exceptions that
  105. have no bearing here) is NOT defined by the Smalltalk syntax. 
  106.  
  107. Also, note that the main logic sequence executes 4 message sends out of 11 
  108. instructions--a rather high ratio for C-code (equating message sends with
  109. function calls, of course), but typical for Smalltalk.
  110.  
  111. What speeds things up is that the bytecodes for each method executed are translated
  112. once into native code, stored in a native method cache, and then directly executed
  113. the next time the same method is executed.  And when the 'factorial' message is 
  114. sent (for example), message selector to method lookup starts by checking whether the 
  115. class of the message receiver is the same as the last time that message was sent to 
  116. that receiver (where "same receiver" is determined lexically).  If so, it can jump 
  117. straight to the correct method. And I do mean "JMP," not "JSR."  So on machines with 
  118. high "JSR" overhead, such as the SPARC I did my benchmarks on, Smalltalk is actually 
  119. faster than C.  This is a reproducible fact.
  120.  
  121. Because of 32-bit overflow (a problem for C, not Smalltalk), the benchmark consists of 
  122. computing the factorial of 12 (=479,001,600) a large number of times (so that execution 
  123. time is about 1 minute).  The time to simply execute an empty loop the same number of 
  124. times is subtracted from the overall time, so that only the time actually spent computing 
  125. the factorial is actually counted.
  126.  
  127. --Alan
  128.